<?php


namespace App\Logic;


use App\Common\lib\Code;
use App\Common\lib\Jwt;
use App\Exception\ApiException;
use App\Models\UserModel;
use EasySwoole\EasySwoole\Config as GlobalConfig;
use EasySwoole\EasySwoole\Logger;

class UserLogic
{
    /**
     * 微信登录逻辑
     * @param array $params
     * @return array
     * @throws ApiException
     * @throws \EasySwoole\HttpClient\Exception\InvalidUrl
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \EasySwoole\WeChat\Exception\MiniProgramError
     * @throws \EasySwoole\WeChat\Exception\RequestError
     * @throws \Throwable
     */
    public function login($params = [])
    {
        $wechat = GlobalConfig::getInstance()->getConf("wechat");
        if (empty($params['code']) || !isset($params['code'])) throw new ApiException("code不存在", Code::$error);
        if (empty($params['avatar'])) throw new ApiException("头像不能为空");
        if (empty($params['nickname'])) throw new ApiException("昵称不能为空");

        $wxa = new \EasySwoole\WeChat\MiniProgram\MiniProgram;
        $wxa->getConfig()->setAppId($wechat['appid'])->setAppSecret($wechat['secret']);
        $session = $wxa->auth()->session($params['code']);
        //判断用户是否登录过了
        $openid = UserModel::create()->get(['openid' => $session['openid']]);
        if (empty($openid)) {
            //注册
            $user = UserModel::create(['openid' => $session['openid'], 'nickname' => $params['nickname'], 'avatar' => $params['avatar'], 'create_time' => date('Y-m-d H:i:s')])->save();
            $token = Jwt::createToken($session['openid']);
        } else {
            $token = Jwt::createToken($session['openid']);
        }
        UserModel::create()->update(['nickname' => $params['nickname'], 'avatar' => $params['avatar'], 'update_time' => date('Y-m-d H:i:s'), 'token' => $token], ['openid' => $session['openid']]);
        return ['openid' => $session['openid'], 'nickname' => $params['nickname'], 'avatar' => $params['avatar'], 'token' => $token];
    }

    /**
     * 用户编辑
     * @param array $params
     * @param array $user
     * @return bool
     * @throws ApiException
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function user_edit($params = [], $user = []): bool
    {
        $data = [];
        if (empty($params['text'])) {
            throw new ApiException('个人说明不能为空', Code::$error);
        }
        if (mb_strlen($params['text']) > 30) {
            throw new ApiException('个人说明不能超过30个字符', Code::$error);
        }
        $data['text'] = $params['text'];
        if (!empty($params['avatar'])) {
            $data['avatar'] = $params['avatar'];
        }
        if (!empty($params['nickname'])) {
            $data['nickname'] = $params['nickname'];
        }
        //更新
        $data['update_time'] = date('Y-m-d H:i:s');
        UserModel::create()->update($data, ['user_id' => $user['user_id']]);
        return true;
    }
}